package main

import (
	"fmt"
	"http"

	"flow-go.googlecode.com/hg/flow"
)

const flowPath = "/flow"

var flowMux = flow.NewMux(flowPath)

func main() {
	http.Handle(flowPath, flowMux)
	http.HandleFunc("/", handler)
	http.ListenAndServe("localhost:8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/" {
		http.Error(w, "not found", http.StatusNotFound)
		return
	}
	flowMux.Become(myFlow, w, r)
}

func myFlow(c flow.Context) {
	for n := 0; ; n++ {
		rw, _ := c.Next()
		fmt.Fprintf(rw, "n = %d<br>", n)
		if n > 5 {
			fmt.Fprint(rw, "Shutting down")
			break
		}
		fmt.Fprintf(rw, `<a href="%s">Continue</a>`, c.URL())
	}
	c.Close()
}
